package data.reader;

import dao.SQLiteConnection;
import gui.reader.ReaderSelectWin;
import java.sql.*;

public class ReaderSelect {
    ReaderSelectWin win;
    String username;
    public ReaderSelect(String a){
        username=a;
        win=new ReaderSelectWin(username);
        //查询按钮
        win.getSelect().addActionListener(e->{
            //获取文本框的内容
            String ISBN = win.getT1().getText();
            String bookname = win.getT2().getText();
            String booktype=win.getT3().getText();
            String author=win.getT4().getText();
            //生成sql语句
            if(ISBN.isEmpty()&&bookname.isEmpty()&&booktype.isEmpty()&&author.isEmpty()){
                //空值检索，不执行操作
            }
            else {
                //非空检索
                String sql="SELECT * FROM Book WHERE";
                if(!ISBN.isEmpty()){
                    sql=sql+" ISBN='"+ISBN+"'";
                }
                if(!bookname.isEmpty()){
                    if(ISBN.isEmpty()){
                        sql=sql+" bookname like '%"+bookname+"%'";
                    }
                    else {
                        sql = sql + " AND bookname like '%" + bookname + "%'";
                    }
                }
                if(!booktype.isEmpty()){
                    if(bookname.isEmpty()){
                        sql=sql+" booktype like '%"+booktype+"%'";
                    }
                    else {
                        sql = sql + " AND booktype like '%" + booktype + "%'";
                    }
                }
                if(!author.isEmpty()){
                    if(booktype.isEmpty()){
                        sql=sql+" author like '%"+author+"%'";
                    }
                    else{
                        sql=sql+" AND author like '%"+author+"%'";
                    }
                }
                System.out.println(sql);
                //连接数据库
                SQLiteConnection connection=new SQLiteConnection();
                connection.Connect_SQLite();
                // 读取数据库中的数据
                try {
                    Connection conn = connection.getConnection();
                    Statement stmt = conn.createStatement();
                    ResultSet rs = stmt.executeQuery(sql);
                    //清除原表格中的内容
                    win.getModel().setRowCount(0);
                    //添加新数据
                    ResultSetMetaData meta = rs.getMetaData();
                    int columnCount = meta.getColumnCount();
                    // 添加数据
                    while (rs.next()) {
                        Object[] rowData = new Object[columnCount];
                        for (int i = 1; i <= columnCount; i++) {
                            rowData[i - 1] = rs.getObject(i);
                        }
                        win.getModel().addRow(rowData);
                    }
                    win.getTable().setModel(win.getModel());    // 设置表格的数据和显示
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
                connection.closeConnection();
            }

        });
        //清除按钮
        win.getEmpty().addActionListener(e->{
            win.getT1().setText(""); // 清除文本框内容
            win.getT2().setText("");
            win.getT3().setText("");
            win.getT4().setText("");
            //清除表格内容
            win.getModel().setRowCount(0);
            //连接数据库
            SQLiteConnection connection=new SQLiteConnection();
            connection.Connect_SQLite();
            // 读取数据库中的数据
            try {
                Connection conn = connection.getConnection();
                Statement stmt = conn.createStatement();
                ResultSet rs = stmt.executeQuery("SELECT * FROM book");
                ResultSetMetaData meta = rs.getMetaData();
                int columnCount = meta.getColumnCount();
                // 添加数据
                while (rs.next()) {
                    Object[] rowData = new Object[columnCount];
                    for (int i = 1; i <= columnCount; i++) {
                        rowData[i - 1] = rs.getObject(i);
                    }
                    win.getModel().addRow(rowData);
                }
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
            connection.closeConnection();
        });
        win.getLogin().addActionListener(e->new MyBorrow(username));
    }

}

